Skip to content

agx: derive a register cap for sampled compute from occupancy - #461

Merged
RyanTheTide merged 7 commits into
aurora-wipfrom
j716c/t2-sample-occupancy
Nov 29, 2026
Merged

RyanTheTide merged 7 commits into
aurora-wipfrom
j716c/t2-sample-occupancy

Conversation

@erikwb

@erikwb erikwb commented Nov 19, 2026 •

Copy link
Copy Markdown

What: Cap register allocation at 160 half-registers for ordinary Vulkan compute shaders with four or more texture reads on encodings with launch-allocated static thread-private storage (AGX3C and AGX3D, where the register file and occupancy table yield at least a 1.5x resident-thread gain: 640 vs 384 threads) when the launch accepts optional spills, retrying from the whole register file if allocation fails, spills exceed the target scratch limit, or optional spill cost exceeds a bounded per-shader and loop-weighted spill-to-texture budget.
Tested: Host x86_64 at c2984e1f32a5 (asahi + poly 328/328, agx_tests 1,640 passed / 1 skipped, SampledComputeRegisterCap.* 9/9). Exact-head c2984e1f32a5 vs babd36d4d477: J716c M5 Max (88,139 compute + texture CTS cases 0 diffs, texture shaderdb identical reported per-case stats and totals with 0 spills, boot/smoke clean, 0 GPU faults; sc_bench/m5probe and Cyberpunk 2077 T2 not run, see Not yet) and J713 M4 Air (AGX3B, cap = 0, 88,139 compute + texture CTS cases 0 diffs, cache-off shaderdb byte-identical across 45,341 shaders, default-boot baseline smoke clean, 0 new dmesg error lines during this PR's Mesa run window); J313 M1 Air exact-head row pending. Prior-merge-base aca2656bc5b1 hardware wins (tb_sampled_n24 -11.1%, tb_sampled_n32 -14.5%, samp_sampled -7.0%; regressions eliminated) and G13G drm-shim (13/13 shaders identical) are in Evidence.
Risk: two-way door · M5/M5 Pro/M5 Max (AGX3C) and M6 (G18G, AGX3D) Vulkan compute shaders (sw_stage == MESA_SHADER_COMPUTE) with >= 4 texture reads and > 160 half-registers whose optional spills stay within the bounded cost guard (<= 192 B, <= 48 scalar stores and <= 48 scalar loads independently, <= 8 loop-weighted spill/fill ops per weighted texture op); M1/M2 (AGX2, lacks static TPS), M4/M4 Pro/A18 Pro (AGX3B, 192-half file), software graphics variants lowered to compute, and Gallium unaffected.
Revert: git revert -m 1 <merge-sha> on aurora-wip; no persistent on-disk, pipeline/shader cache (hk_cache_identity() hashes build_sha, device generation contract, and dev->params.features), UAPI, or firmware state is changed.
Not yet: J716c (M5 Max / G17C B1) exact-head paired sampled-compute A/B (sc_bench / m5probe probe source and binaries were unavailable on the Mac and build host) and Cyberpunk 2077 T2 A/B (did not fit the 2026-11-28 pass); exact-head hardware run at c2984e1f32a5 on J313 (M1 Air / G13G B1: live AGX2 cap = 0 confirmation and boot/smoke check); plus external G18G (M6) spilling direct+indirect launch verification and external M1 Pro/M2/M4 Pro/A18 Pro runs (no external hardware in lab).

Details

Problem

Compute programs with four or more texture reads spend much of their time waiting on texture fetches, and keeping more threads resident hides that wait. Without a register cap on targets with large per-thread register files (252 halves), these programs allocate near the full file and fall to 384 resident threads per core.

Hardware A/B testing of an unbounded 160-half cap across G13G (AGX2) and G17C (AGX3C) exposed two separate spill-regression mechanisms:

  1. ISA spill architecture (AGX2 vs AGX3 static TPS): AGX2 spills to a helper-backed device-memory scratch BO (agx_scratch) with synchronous 2-slot scoreboard wait stalls after every reload (agx_insert_waits), whereas AGX3 lowers spills to launch-allocated static thread-private storage (copy/rld with hardware scoreboarding and no wait instructions). On G13G, capping to 160 halves slowed ALU-heavy sampled compute by +16% to +259% (up to 3.6x, vs +8.7% to +47.9% on G17C) and yielded no meaningful gain on texture-bound workloads (-0.9% to +2.4%, vs -7.0% to -14.5% on G17C for the same shaders).
  2. Unbounded optional spills on AGX3C: Capping solely on samples >= 4 forced 82–290 scalar spills/fills on ALU-heavy compute shaders with only 8 texture reads (regressing G17C by +8.7% to +47.9%) and added 54–78 spills (216–312 B scratch) on heavy texture shaders for 0% gain.

Change

Cap register allocation for ordinary Vulkan compute programs with four or more AGX_OPCODE_TEXTURE_SAMPLE / AGX_OPCODE_TEXTURE_LOAD instructions for an occupancy-model target of 640 resident threads (5/8 of 1024 maximum), trading bounded static TPS spills for occupancy:

  • Derived from static TPS, file, and occupancy (src/asahi/compiler/agx_performance.c): agx_isa_has_static_tps() checks whether the encoding uses launch-allocated static TPS (AGX3B, AGX3C, AGX3D), agx_register_file_halves() gives the per-thread file for each encoding, and agx_sampled_compute_register_cap() takes the largest budget reaching the 640-thread step (occupancies[0].max_threads * 5 / 8). A target gets the cap only where it has static TPS and the occupancy model raises resident threads by at least 1.5x (capped * 2 >= uncapped * 3): AGX3C and AGX3D (252 halves, 384 -> 640) resolve to 160 halves; AGX3B (192 halves, 512 -> 640, which lost 7–10% on G16G), AGX2 (helper-backed stack + explicit waits), and unknown encodings return 0.
  • Launch scratch contract and stage scoping (src/asahi/vulkan/hk_cmd_buffer.h, hk_cmd_dispatch.c, hk_shader.c): optional_spills in struct agx_shader_key marks shaders whose launch takes the scratch of optional spills. Borealis sets it only on non-internal real Vulkan compute shaders (!nir->info.internal && sw_stage == MESA_SHADER_COMPUTE && hk_compute_launch_takes_scratch(&dev->dev)), excluding software vertex/tessellation-evaluation variants that hk_compile_nir() lowers to MESA_SHADER_COMPUTE. hk_compute_launch_takes_scratch() matches the compute dispatch launch contract (hk_dispatch_m5_native() requires DRM_ASAHI_FEATURE_COMPUTE_RESOURCES, while hk_dispatch_g17g_native() accepts G18G scratch frames up to 64 KiB via g18_compute_resources). Gallium leaves optional_spills false (src/gallium/drivers/asahi/agx_state.c). hk_cache_identity() already hashes the device generation contract (hk_hash_device_generation_contract()) and all 64 bits of dev->params.features (src/asahi/vulkan/hk_physical_device.c).
  • Allocation, spill-cost guard, and whole-file fallback (src/asahi/compiler/agx_register_allocate.c, agx_compile.c, agx_compiler.h): Helpers, preambles, unspillable programs, and ray-traversal programs skip the cap. When agx_ra() tightens max_possible_regs (sampled_cap < max_possible_regs), it sets ctx->sampled_cap_applied. In agx_compile_function_nir() (called by agx_compile_shader_nir_internal()), if a capped attempt fails register allocation (ctx->ra_failed, including ctx->ra_mem_exhausted), trips agx_sampled_cap_spills_excessive(ctx) (RA spill frame > 192 B, > 48 scalar RA stores or > 48 scalar RA loads checked independently, or loop-weighted scalar RA spill+fill ops > 8 * loop-weighted texture ops, counting per loop header so multi-backedge loops are not double-counted and ignoring NIR scratch offsets < ctx->spill_base_B), or exceeds the AGX3 scratch-frame limit, it retries once with disable_sampled_cap = true and ra_budget_cut = 0 from the whole register file before falling back to ra_budget_cut retries or refusal.
  • Branch history: Merged origin/aurora-wip (262403b7bce1) into b2392e5ccd5f at 22f040c4636d, dropped the redundant G18G scratch guard and bit-31 compiler flag in a064bf402c15, added the static-TPS gate, post-spill cost guard, and sw_stage == MESA_SHADER_COMPUTE scoping in aca2656bc5b1, and merged aurora-wip (babd36d4d477) at c2984e1f32a5 (conflict-free against the PR's 10 touched files; git merge-tree --write-tree c2984e1f32a5 46f3a68e60d9 also merges cleanly with identical +858/-5 diff across those 10 files).
Evidence
  • Exact-head J716c (M5 Max, T6050, 2026-11-28): native release base-plus-patch builds (aurora-wip 46f3a68e60d9 incremental tree plus each arm's patch, sharing git-97742505d8 in driverInfo and identified by source SHA and libvulkan_asahi.so SHA-256: 7f6f76e89599829e6c8196abe24d3234243c47634cc0d619a9a18266bbf63522 for head c2984e1f32a5 vs 51a29f064fd26295d4b67501501fd319337cba51f27fe87c803f1c7affe9c30d for base babd36d4d477); default kernel image SHA-256 bf2582ab357573db64031f39e7bc9d617eb574c6b221364ff417810bbddf4c87, default Stage 2 SHA-256 21edee972e1ff1718c6f41c32b91ee151afdc58cbb69f16f2f6a4d8d4c722170, AC power. CTS runs used tools/cts/aurora-cts-ab.py (6 workers, shader cache on, chunk 300, CTS 7ba03cdac7ae) with its case-list expansion patched locally to keep only TEST: lines (the repository copy mis-parses this CTS build's export); Borealis arms are selected through VK_DRIVER_FILES.
    • dEQP-VK.compute.*: 60,838 cases on both arms, 15,525 Pass / 45,313 NotSupported, 0 Fail / Crash / DeviceLost, 0 diffs after retry (base 2m22s, head 2m21s).
    • dEQP-VK.texture.*: 27,301 cases on both arms, 19,440 Pass / 7,861 NotSupported, 0 diffs; --shaderdb (shader cache off) over the same cases reported identical per-case stats and totals (30,616 shaders, instrs 1,339,729, code_size 11,516,120, uniforms 2,446,326, 0 spills / fills / scratch on both arms, 0 of 27,301 cases changed).
    • The paired sc_bench / m5probe sampled-compute A/B (tb_sampled_n24, tb_sampled_n32, samp_sampled) did not run because those probe sources/binaries were unavailable on the Mac and build host; Cyberpunk 2077 T2 A/B also did not run because it did not fit the pass. The prior-merge-base wins stay labeled as such below.
    • boot/smoke on the default boot: eDP-1 3456x2234@120 enabled; Apple MTP keyboard, trackpad and SMC lid devices enumerated (not exercised remotely); wlan0 connected, 0% loss over 3 pings; AppleJ716 card present with speakerguardd v2 active (no playback in this row); Borealis Apple M5 Max (G17C B1) with vkcube --c 240 --wsi wayland clean; 0 failed units (system and user); 0 LSM denials (no SELinux/AppArmor loaded); 0 GPU fault, MMU fault, SError or Oops lines. Redacted dmesg excerpt (default boot, unchanged by the runs): aurora 2302600000.gpu: GPU firmware: 27.2 (26B123), G17C; 0 GPU fault / MMU fault / DeviceLost lines after all runs; the err+ set is the boot's usual 55 lines (apple-dcp ... unable to match service ... and systemd-gpt-auto-generator), unchanged.
  • Exact-head J713 (M4 MacBook Air, T8132, G16G B0, AGX3B, 2026-11-28): native release base-plus-patch builds (aurora-wip 46f3a68e60d9 plus each arm's patch, built on the aarch64 lab sibling Mac, sharing git-97742505d8 in driverInfo and identified by source SHA and libvulkan_asahi.so SHA-256: 7f6f76e89599829e6c8196abe24d3234243c47634cc0d619a9a18266bbf63522 for head c2984e1f32a5 vs 51a29f064fd26295d4b67501501fd319337cba51f27fe87c803f1c7affe9c30d for base babd36d4d477), selected via VK_DRIVER_FILES on battery; default Linux 7.3.0 (default J713 7.3.0 kernel) kernel image SHA-256 592f7bc8834d8796d5bdb086091e90ac96fc6a17a83769cbadc92b3268604c49, default Stage 2 SHA-256 3b2d2f059858d1891b225910864c02d68afb1b42893667e0afa8666491651071; CTS 7ba03cdac7ae, 3 workers (aurora-cts-ab 1.0).
    • dEQP-VK.compute.* + dEQP-VK.texture.* (all 88,139 cases): 27,864 Pass / 60,275 NotSupported on both arms, 0 diffs.
    • --shaderdb (shader cache off) on the same 88,139 cases: 45,341 shaders on both arms, instrs 2846827, code_size 28265460, spills 2256, fills 2892, uniforms 2773256, scratch 9024 (byte-identical, +0 on every total, 0 of 88,139 cases changed), as expected on AGX3B (cap = 0).
    • Default-boot baseline smoke (default J713 7.3.0 kernel): eDP-1 2560x1664@60 enabled, input enumerated, wlan0 3/3 gateway ping (0% loss), speakerguardd-v2 active with protected-sink sgv2.voicing tone rc 0, Borealis Apple M4 (G16G B0) Vulkan 1.4.363 (glmark2 5,047), 0 failed units (system and user), 0 AppArmor denials (no SELinux), 4 pre-existing baseline err+ boot lines (AFK[ep:20], unsupported DFB layout, apple_sep matching, brcmfmac ring 4). Per-arm Mesa vulkaninfo --summary / vkcube smoke passed with 0 new dmesg error lines during this PR's Mesa run window.

Host build and unit tests at head c2984e1f32a5

  • Configuration: x86_64, -Dvulkan-drivers=asahi -Dgallium-drivers=asahi -Dplatforms=wayland -Dglx=disabled -Dbuild-tests=true -Dtools=drm-shim -Doptimization=2 -Ddebug=false -Db_ndebug=false.
  • Commands: ninja -C _build && meson test -C _build --suite asahi --suite poly && _build/src/asahi/compiler/agx_tests --gtest_filter='SampledComputeRegisterCap.*'.
  • Results: 328/328 passed (asahi + poly), 0 failed; agx_tests 1,640 passed, 1 pre-existing skipped (NativeFMA.ExactDyadicOracle), 0 failed; SampledComputeRegisterCap.* (src/asahi/compiler/test/test-sampled-compute-register-cap.cpp) 9/9 passed (DerivedFromFileAndOccupancy, RetryAfterFailedAllocationUsesWholeFile, NoCapOnAGX2WithoutStaticTPS, NoCapWithoutOptionalSpills, NoCapBelowTextureThreshold at 0 and 3 loads, RetryOnSpillExhaustionUsesWholeFile, ExcessiveSpillsRetryUsesWholeFile, ModerateSpillsKeepSampledCap, LoopSpillsWeightedAgainstLoopTextures covering multi-backedge loops, nested loops, non-zero spill_base_B, and 64-bit scalar boundary).
  • Merge check: babd36d4d477..c2984e1f32a5 PR diff (+858/-5 across 10 files) matches 262403b7bce1..aca2656bc5b1 logic; babd36d4d477 merged 77 commits into aurora-wip including shared src/asahi/compiler/ and src/asahi/vulkan/ files.

Prior-head host build and unit tests at aca2656bc5b1

  • Configuration: x86_64, -Dvulkan-drivers=asahi -Dgallium-drivers=asahi -Dplatforms=wayland -Dglx=disabled -Dbuild-tests=true -Dtools=drm-shim -Doptimization=2 -Ddebug=false -Db_ndebug=false.
  • Commands: ninja -C _build && meson test -C _build --suite asahi --suite poly.
  • Results: 327/327 passed (asahi 322/322, poly 5/5), 0 failed; agx_tests 1,621 passed, 1 pre-existing skipped (NativeFMA.ExactDyadicOracle), 0 failed; SampledComputeRegisterCap.* (src/asahi/compiler/test/test-sampled-compute-register-cap.cpp) 9/9 passed (DerivedFromFileAndOccupancy, RetryAfterFailedAllocationUsesWholeFile, NoCapOnAGX2WithoutStaticTPS, NoCapWithoutOptionalSpills, NoCapBelowTextureThreshold at 0 and 3 loads, RetryOnSpillExhaustionUsesWholeFile, ExcessiveSpillsRetryUsesWholeFile, ModerateSpillsKeepSampledCap, LoopSpillsWeightedAgainstLoopTextures covering multi-backedge loops, nested loops, non-zero spill_base_B, and 64-bit scalar boundary).

Prior-head hardware runs at aca2656bc5b1 vs 262403b7bce1 (pre-babd36d4d477 merge)

Both aca2656bc5b1 Mac runs used the same cross-compiled aarch64 Borealis release libraries (-Dvulkan-drivers=asahi -Dgallium-drivers=[] -Dplatforms=wayland -Dglx=disabled -Doptimization=2 -Ddebug=false -Db_ndebug=false):

  • Baseline arm (262403b7bce1): driver identity reported on device Borealis Mesa 26.3.0-devel (git-262403b7bc); staged libvulkan_asahi.so sha256 da99d77ce72e606a18dbee76624c9503601282125e6909ba32a106bafe302c36 (also device-verified via sha256sum on J713).
  • Candidate arm (aca2656bc5b1): driver identity reported on device Borealis Mesa 26.3.0-devel (git-aca2656bc5); staged libvulkan_asahi.so sha256 3ee76d3a76ee20a0e6a8036593f2f70ccb8b88a4f24483cb406b3606a4cdb969 (also device-verified via sha256sum on J713).
  • CTS build: VK-GL-CTS commit 7ba03cdac7ae (vk-default mustpass, deqp-vk --deqp-surface-type=pbuffer --deqp-log-images=disable --deqp-log-shader-sources=disable --deqp-terminate-on-fail=disable, 6 parallel shards, fresh per-arm MESA_SHADER_CACHE_DIR, diagnostics off).

1. J716c (M5 Max / G17C B1, Linux 7.3.0, Arch ARM)

  • Paired A/B commands: Interleaved 6 rounds (base/fix alternating order across rounds r1–r6), 20 timed dispatches per round, MESA_SHADER_CACHE_DISABLE=true, diagnostics off (sc_bench <shader.spv> 20 for sc_sampled_*/tb_sampled_*; m5probe --reps 20 samp <name> for samp_sampled/samp_control). Output checksums were bit-identical across every arm and round.
  • Paired A/B summary (median of 6 round medians, with [min..max] across the 6 round medians):
Shader Tex reads Base (262403b7bce1) GPRs/spills Prior a064bf402c15 delta (spills) Head aca2656bc5b1 GPRs/spills Base med [min..max] (us) Head med [min..max] (us) Delta (aca2656bc5b1) Rounds faster
sc_sampled_n20 8 163 / 0:0 -0.9% (22:22) 159 / 22:22 (32 B) 256.4 [252.6..258.4] 254.8 [252.6..259.4] -0.6% 3/6
sc_sampled_n24 8 195 / 0:0 +8.7% (82:82) 195 / 0:0 (uncapped) 250.1 [245.1..257.3] 254.3 [247.4..258.1] +1.7% 2/6
sc_sampled_n28 8 227 / 0:0 +18.1% (136:136) 227 / 0:0 (uncapped) 263.9 [260.8..265.1] 264.8 [263.0..270.4] +0.3% 1/6
sc_sampled_n32 8 247 / 29:29 +20.2% (192:192) 247 / 29:29 (uncapped) 300.6 [298.9..302.4] 300.3 [298.3..303.8] -0.1% 4/6
sc_sampled_n40 8 247 / 125:125 +47.9% (290:290) 247 / 125:125 (uncapped) 395.6 [384.0..397.5] 380.8 [376.0..391.0] -3.7% 6/6
tb_sampled_n24 24 207 / 0:0 -11.2% (30:30) 159 / 30:30 (128 B) 4106.2 [4099.1..4117.0] 3649.1 [3647.6..3654.3] -11.1% 6/6
tb_sampled_n32 32 231 / 0:0 -14.5% (30:30) 159 / 30:30 (120 B) 5786.2 [5779.0..5792.6] 4947.8 [4945.0..4956.4] -14.5% 6/6
tb_sampled_n40 40 247 / 10:10 -0.1% (54:54, 216 B) 247 / 10:10 (uncapped) 6125.5 [6108.0..6140.5] 6120.1 [6108.6..6123.3] -0.1% 5/6
tb_sampled_n48 48 247 / 34:34 -0.0% (78:78, 312 B) 247 / 34:34 (uncapped) 7350.0 [7343.3..7374.6] 7363.5 [7341.6..7402.4] +0.2% 1/6
samp_sampled 52 231 / 0:0 -7.0% (29:29) 159 / 29:29 (116 B) 4753.6 [4753.0..4755.1] 4422.1 [4381.8..4425.0] -7.0% 6/6
samp_control 0 197 / 0:0 +0.0% (0:0) 197 / 0:0 (uncapped) 54.5 [54.5..54.6] 54.5 [54.5..54.5] -0.0% 3/6
  • Raw per-round medians (us, r1..r6, base vs head) for the 3 capped winners:
    • tb_sampled_n24: base 4099.6, 4099.1, 4100.4, 4117.0, 4112.0, 4112.6 vs head 3647.6, 3654.0, 3648.9, 3654.3, 3648.2, 3649.2 (checksum c7ccd04d10a4a7a3)
    • tb_sampled_n32: base 5792.6, 5789.5, 5785.3, 5779.0, 5787.2, 5781.3 vs head 4945.6, 4946.2, 4945.0, 4949.4, 4950.0, 4956.4 (checksum f1d270bf0f1bb6e3)
    • samp_sampled: base 4755.12, 4753.39, 4753.82, 4753.14, 4753.01, 4753.74 vs head 4420.67, 4423.54, 4424.97, 4381.76, 4424.38, 4420.53 (fnv 02691a7b)
  • CTS (VK-GL-CTS 7ba03cdac7ae, 6 shards, per-arm cold cache):
    • dEQP-VK.compute.* (60,838 cases): 15,525 Pass / 45,313 NotSupported / 0 Fail / 0 Crash / 0 Timeout on both arms (0 diffs; 163 s vs 164 s).
    • dEQP-VK.texture.* (27,301 cases): 19,440 Pass / 7,861 NotSupported / 0 Fail / 0 Crash / 0 Timeout on both arms (0 diffs; 46 s vs 45 s).
    • Total: 88,139 cases ran, 0 diffs.
  • System health: 0 GPU error lines in dmesg (dmesg_delta_lines=4, non_audit_lines=1), 0 AVC lines (SELinux not built on Arch ARM), 0 new failed units (2 pre-existing host units already failed before the run and unchanged afterward: aurora-peripherals.service, systemd-networkd-wait-online.service).

2. J713 (M4 Air / G16G B0, AGX3B, cap = 0, Linux 7.3.0, Arch ARM)

  • Driver identities verified on device: git-262403b7bc (sha256sum da99d77ce72e606a18dbee76624c9503601282125e6909ba32a106bafe302c36) vs git-aca2656bc5 (sha256sum 3ee76d3a76ee20a0e6a8036593f2f70ccb8b88a4f24483cb406b3606a4cdb969).
  • Uncached AGX_MESA_DEBUG=shaderdb (MESA_SHADER_CACHE_DISABLE=true):
    • 9 benchmark compute probes (sc_sampled_n20..40, tb_sampled_n24..48): 9/9 shaders byte-identical (0 diffs), bit-identical output checksums (4b404aa0a9cf4383).
    • dEQP-VK.compute.pipeline.basic.* (80 cases, 85 shader lines): 85/85 byte-identical (0 diffs).
  • CTS (VK-GL-CTS 7ba03cdac7ae, 6 shards, per-arm cold cache, diagnostics off):
    • dEQP-VK.compute.* (60,838 cases): 13,140 Pass / 47,698 NotSupported / 0 Fail / 0 Crash / 0 Timeout on both arms (0 diffs; 11 s vs 11 s).
    • dEQP-VK.texture.* (27,301 cases): 6-shard pass reported 14,718 Pass / 12,577 NotSupported / 6 Fail on both arms (6 symmetric parallel-shard flakes in dEQP-VK.texture.explicit_lod.2d.formats.*: 3 Fail->Pass, 3 Pass->Fail); isolated cold-cache rerun of all 6 cases returned 6/6 Pass / 0 Fail on both arms (14,724 Pass / 12,577 NotSupported / 0 Fail, 0 net diffs).
    • Total: 88,139 cases ran, 0 net diffs.
  • System health: 0 GPU error lines in journalctl -k over the run window, 0 AVC lines (SELinux not built on Arch ARM), 0 failed units before and after (systemctl --failed empty).

3. Offline drm-shim G13G (AGX2, cap = 0) zero-change check at aca2656bc5b1 vs 262403b7bce1

  • ASAHI_SHIM_CHIP=g13g, MESA_SHADER_CACHE_DISABLE=true, AGX_MESA_DEBUG=shaderdb: all 13/13 shaders (sc_sampled_n18..40, tb_sampled_n16..48, samp_sampled, samp_control) produce byte-identical shaderdb output (0 diffs; agx_sampled_compute_register_cap() == 0 on AGX2).

Earlier-head hardware runs (superseded by aca2656bc5b1 and c2984e1f32a5)

  • J313 (M1 Air / G13G B1, a064bf402c15 vs 262403b7bce1): dEQP-VK.compute.* + texture.* (88,139 cases, 0 diffs) + image.* (135,825 cases, 0 diffs, pre-existing 6,640 DeviceLost / 36 Fail identical on both arms); generated ALU-heavy sampled compute regressed +16% to +259% (up to 3.6x) and texture-bound ran -0.9% to +2.4% when AGX2 had the 160-half cap enabled — motivating the AGX2 static-TPS gate and spill-cost guard in aca2656bc5b1.
  • Earlier G17C Cyberpunk 2077 T2 runs (ca677d21601b on afcc08b71067 + #450, predating the spill-cost guard): 10 consecutive T2 launches (2560x1600 High) completed at 28.49–28.97 FPS vs 27.77/28.50 FPS control, with the two hot 8x8 compute shaders at 29 and 33 spills (an exact-head AC re-run at c2984e1f32a5 is listed under Not yet).

@RyanTheTide

Copy link
Copy Markdown

Thanks for this register-pressure experiment.

We select behaviour by a hardware property rather than by the chip a tuning was measured on, and we ship tuning defaults only with a measured gain. Here the cap is tied to G17C:

  • src/asahi/compiler/agx_register_allocate.c:3100-3110 if (ctx->key->dev.chip == AGX_CHIP_G17C && ... ) max_possible_regs = MIN2(max_possible_regs, 160);

The short replay in the description measured -4.2 % FPS with this change. Could you share the measurement that shows the occupancy gain? If it holds, the threshold should follow the hardware property that sets occupancy (register file size per core and the wave-occupancy steps), so G17G, G17S and the AGX3B parts get the same rule, with a code comment naming the reason for any generation left out.

If you make it the default, we can supply rows on our M1 Pro, M2, M4 Pro and A18 Pro.

@erikwb

erikwb commented Nov 19, 2026 •

Copy link
Copy Markdown
Author

Restoring the 160-half-register cap alone improved full-game T2 by +0.14 and +0.76 FPS in the two blocks. The direction repeated, but the size varied. One launch exited with a userspace allocator error before producing a score. Its replacement completed in the same position in the sequence.

We used two blocks of full Cyberpunk T2 runs in opposite orders on the M5 Max, native 2560×1600 High, with the fans fixed. The control was Mesa e59727e7a4fc with #460, #461, #466 and #468 removed. Each delta uses the mean of two control runs in its block.

For two hot 8×8 compute shaders the compiler’s maximum resident-thread estimate rose from 512 to 640. Each shader went from zero static spills/reloads to 29/33. That is the occupancy/spill trade-off behind the change. The 640 figure is a compiler estimate. Measuring achieved occupancy would require a runtime counter. A portable threshold would need the register-file and occupancy steps for each GPU.

In a separate eight-run comparison, enabling #460, #461, #466 and #468 together raised native T2 from 29.99 to 31.33 FPS. All four adjacent pairs favored the bundle. That 1.35 FPS is the combined effect.

@RyanTheTide

Copy link
Copy Markdown

Thanks for the isolated runs and the occupancy numbers. The direction is encouraging, though +0.14 vs +0.76 FPS is a wide spread, and the launch that exited with an allocator error is worth understanding before this becomes a default.

On scope: the cap at agx_register_allocate.c:3100-3110 is still keyed on AGX_CHIP_G17C. As you note, a portable threshold follows from the register file and the occupancy steps; could the cap be derived from those values (even if only G17C's are filled in at first), so the other M5 parts and G17P pick it up from their own data, with a comment naming why any generation is left out? We can supply rows on our M1 Pro, M2, M4 Pro and A18 Pro.

@DjDeveloperr

Copy link
Copy Markdown

Merge train: independent review (code only, Opus 5.5 xhigh)

Verdict: CLEAN WITH NITS. No blocker or major. NITs: the 160-half cap is an unnamed constant, and it uses the shared G13-era occupancy table. The allocator error in one of your T2 runs is still unexplained, so worth understanding before this becomes a default.

Reach: gated on chip == AGX_CHIP_G17C, and no libagx target compiles as G17C, so this changes no codegen, layouts or cache keys on G17S/G17G (M5 / M5 Pro) or G17P. The train holds it only on RyanTheTide's open property-gate request (owner decision pending on merging G17C-gated PRs as-is). On widening: keying on the AGX3C encoding would also sweep in G18G (encoding AGX3C, chip G17G, emit_agx3d), and any encoding-wide gate changes the precompiled g17g/g18g libagx kernels every M5 part loads. For this PR, check that spilled code can actually run on G18G.

@erikwb
erikwb force-pushed the j716c/t2-sample-occupancy branch from 14c2f5f to e2dcb3c Compare November 21, 2026 00:09
@erikwb erikwb changed the title agx: limit register pressure in sampled G17C compute shaders agx: derive a register cap for sampled compute from occupancy Nov 21, 2026
@erikwb

erikwb commented Nov 21, 2026 •

Copy link
Copy Markdown
Author

The cap is now derived from the register file and the occupancy steps instead of keyed on G17C (e2dcb3c).

  • agx_register_file_halves() gives the per-thread file for each ISA encoding, and agx_sampled_compute_register_cap() takes the largest budget that reaches 5/8 of the maximum resident threads. It applies only where that raises resident threads by half again or more.
  • Result with the current occupancy table: AGX2 and AGX3C get 160 half-registers (G17C goes from 384 to 640 threads). AGX3B gets none: on an M4 (G16G) the same cap went from 512 to 640 threads and lost 7-10% on the sample workload.
  • @DjDeveloperr on G18G: it gets no cap. The derivation returns 0 for G18G, and the new optional_spills key bit is false there, because its launches refuse scratch. The bit is also false on M5 without the kernel's compute resources, and in Gallium. The 160 is no longer an unnamed constant.
  • The allocator error in one T2 launch is not explained yet. The game exited before scoring and the rerun in the same position completed; I have not reproduced it.
  • AGX2 (M1/M2) now gets the cap and is untested. Rows from your M1 Pro and M2 would cover it, plus A18 Pro and M4 Pro for no regression.

M4 (G16G), where the cap does not apply: the compute shaders from dEQP-VK texture and compute subsets (5,535) compile unchanged and give the same results. The PR body has the details.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blockers in the code. Re-review of e2dcb3c: the cap is now derived from the register file and the occupancy steps, which resolves our scope point, and the AGX3B exclusion has a measured reason. The capped * 2 < uncapped * 3 test gives 160 on AGX2/AGX3C and none on AGX3B, as described. Bit 31 of the compiler flags is free (debug bits stop at 25, FS structure is bit 30).

  1. Reach: AGX2 (M1/M2) Vulkan compute with 4 or more texture reads now gets the 160-half cap and its spills. There is no run on AGX2, and the one T2 launch that exited with an allocator error is still unexplained. That launch used the earlier G17C-only cap, so it is the same mechanism. Before this becomes a default on more chips, please either reproduce that error or rule it out, for example by running T2 or the replay repeatedly on the cap build.
  2. src/asahi/vulkan/hk_shader.c:5432 optional_spills is set from chip checks (!= AGX_CHIP_G18G, agx_chip_g17_m5) that repeat which dispatch paths accept scratch. If hk_dispatch_m5_native() or hk_dispatch_g17g_native() changes, this goes stale and a capped shader with spills could be launched without scratch. Suggest one helper next to those dispatch paths (for example hk_compute_launch_takes_scratch(dev)) that both use.
  3. Naming: the PR body says "Honeykrisp sets it for compute" and "AArch64 release Honeykrisp". Please call the driver Borealis in new prose.

HW rows we can run: M1 Pro and M2: dEQP-VK.compute.* and dEQP-VK.texture.* subsets plus dEQP-VK.image.*, base vs head, no new failures; and a texture-heavy compute A/B (one of your sample workloads, if you can share it). Pass = no regressions and no slowdown. A18 Pro and J616s: no codegen change expected, so a shader-stats diff of the same set should show zero changes.

Assisted-by: LLM (Claude:Opus 5.5)

@erikwb
erikwb force-pushed the j716c/t2-sample-occupancy branch from e2dcb3c to 8e4db9d Compare November 21, 2026 16:32
@erikwb

erikwb commented Nov 21, 2026 •

Copy link
Copy Markdown
Author

Thanks. All three points are addressed in ca677d2.

2. One helper for the scratch rule. hk_compute_launch_takes_scratch(dev) now sits in hk_cmd_buffer.h, next to hk_dispatch_with_local_size(). It returns false on G18G and, on M5, true only with the kernel's compute resources. Four places use it:

  • optional_spills in hk_compile_nir();
  • the shader cache identity bit in hk_physical_device_compiler_flags();
  • the scratch refusal in hk_dispatch_m5_native();
  • the scratch refusal in hk_dispatch_g17g_native().

A change to which launches take scratch now moves the compiler bit, the cache key and the dispatch refusal together. hk_dispatch_g17g_native() still refuses preamble GPRs on G18G as before. That check is now separate from the scratch check.

3. Naming. The body says Borealis.

1. The allocator error. It did not reproduce. I ran ten consecutive T2 launches of this head on the M5 Max (aurora-wip afcc08b71067 + #450, Linux 7.3.0-ewb270, issue #457 method). All ten scored, at 28.49 to 28.97 FPS, with no heap errors and no GPU faults in the kernel log. Two base launches between them scored 27.77 and 28.50 FPS. This kernel is slower than the one in the earlier rows for both builds.

The earlier failure was a glibc heap-corruption abort, double free or corruption (out) followed by free(): chunks in smallbin corrupted. It was not a GPU memory allocation failure. The game launch logs on this machine since 2026-11-18 contain the same abort family (double free or corruption, corrupted double-linked list, malloc(): smallbin double linked list corrupted) in 10 of about 1,440 launches, each on a different driver build. One of them is the plain control build of that study (92c9f438a005), which has no register cap at all. So the same abort occurs without the cap, and it predates this change. Ten clean launches don't rule out an effect on its rate: at about 1 in 150 launches, ten clean runs would be expected about 93% of the time either way. I have not found its cause.

Tests: all Mesa tests pass in a debug-optimized build with assertions (192/192), including agx_tests and the cap test.

M1 Pro/M2 rows and the shader-stat diff on A18 Pro/J616s would still be welcome.

@RyanTheTide

Copy link
Copy Markdown

Re-review of 8e4db9d: Blockers: 1.

From our last comment:

  1. Partly resolved. The heap abort is ruled out as specific to the cap: the same glibc abort family appears in launches of a control build with no cap, and ten consecutive T2 launches of this head scored with no heap errors. Still open: AGX2 (M1/M2) now gets the 160-half cap and its spills on every Vulkan compute program with 4 or more texture reads, and has no run. The 5/8 target and the "half again" step were tuned on G17C and G16G only, so AGX2 needs its own A/B before this lands.
  2. Resolved: hk_compute_launch_takes_scratch() (src/asahi/vulkan/hk_cmd_buffer.h:2885) now drives optional_spills (hk_shader.c:5430), the cache identity bit (hk_shader.c:214) and both dispatch refusals (hk_cmd_dispatch.c:1808, :2568). The changed refusal in hk_dispatch_g17g_native() no longer has a g18 && guard, but that function is only called for G18G (hk_cmd_buffer.h:2903), so G17G behaviour does not change.
  3. Resolved: the description says Borealis.

New since our comment:

  1. The commit message ends with "Developed with Claude Code." The commit should carry only your identity and Signed-off-by; the PR description already has the Assisted-by line. Please drop that line from the commit message.
  2. Not blocking: src/asahi/compiler/agx_performance.c:101 still excludes emit_agx3d/AGX3D inside the derivation because of G18G's dispatch path. optional_spills now carries that launch rule (false on G18G via the helper), so this check repeats it in the compiler and keeps the cap off for any future AGX3D part even if its launches take scratch. Dropping it keeps the cap purely file and occupancy derived; the G18G test case can then check a key without optional_spills instead.

HW row: M1 Pro (G13S) and M2 (G14G), base against this head: dEQP-VK.compute.*, dEQP-VK.texture.* and dEQP-VK.image.* with no new failures and no GPU fault lines, plus a texture-heavy compute A/B (one of your sample workloads) with no slowdown. A18 Pro and J616s: a shader-stats diff of the same sets showing zero changes. We can run these.

Assisted-by: LLM (Claude:Opus 5.5)

@erikwb
erikwb force-pushed the j716c/t2-sample-occupancy branch from 8e4db9d to ca677d2 Compare November 21, 2026 17:11

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of ca677d2: Blockers: 1, unchanged from our last comment.

The only change from 8e4db9d is the commit message. "Developed with Claude Code." is gone, so that point is resolved, and the code is identical on the same base afcc08b. Still open: the AGX2 (M1 Pro / M2) A/B for the 160-half cap. The agx_performance.c:101 AGX3D exclusion is non-blocking, as before. The HW row in our last comment still applies.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of ca677d21: 0 code, attribution, or policy blockers remain.

Verification

  • Occupancy-derived cap (src/asahi/compiler/agx_performance.c:43-122): agx_register_file_halves() and agx_sampled_compute_register_cap() derive the 160-half cap from the 5/8 resident-thread target and the >= 1.5x occupancy gain check (160 on AGX2/AGX3C, 0 on AGX3B where 512 -> 640 regressed G16G by 7-10%).
  • Scratch contract & cache key (hk_cmd_buffer.h:2885, hk_cmd_dispatch.c:1808, :2568, hk_shader.c:214, :5429): hk_compute_launch_takes_scratch() unifies optional_spills, the shader cache identity bit (bit 31, verified free on current aurora-wip where AGX_DBG_* ends at bit 25 and FS structure uses bit 30), and the M5/G18G dispatch scratch refusals.
  • RA retry & tests (agx_register_allocate.c:3123-3137, test-sampled-compute-register-cap.cpp): ra_failed retries with ra_budget_cut = 16, dropping sampled_cap to allocate from the full file; unit tests cover per-encoding caps, retry fallback, and !optional_spills (192/192 Mesa tests pass).
  • Attribution & naming: single human-signed commit (no AI trailers in commit), Borealis naming in PR body, Assisted-by: at end of PR body. Earlier glibc heap abort is reasonably ruled out as cap-specific (present in control builds; 10 consecutive clean T2 launches on ca677d21).

Non-blocking code observation

  • src/asahi/compiler/agx_performance.c:101: if (dev->emit_agx3d || dev->encoding == AGX_ISA_ENCODING_AGX3D) return 0; duplicates the G18G launch-scratch rule that optional_spills now conveys (echoing @RyanTheTide's note). Dropping it keeps the compiler helper purely file/occupancy-derived.

Remaining before APPROVE (§5 HW/runtime evidence on ca677d21)

  • AGX2 (M1 Pro / M2) conformance + same-machine A/B performance run: the 160-half cap is live by default on AGX2 Vulkan compute shaders with >= 4 texture reads and currently has zero hardware runs. Given the 7-10% regression on G16G and -4.16% on the short M5 replay, §5 requires unchanged conformance (dEQP-VK.compute.*, texture.*, image.*, 0 GPU faults) and a texture-heavy compute A/B on AGX2 before shipping it as a default.
  • (Also helpful on ca677d21) A18 Pro / J616s zero-change shader-stats confirmation, and a fuller G17C compute/texture CTS run on this head (earlier head ran 54 focused cases).

Assisted-by: LLM (Gemini 4 Neon)

@erikwb
erikwb marked this pull request as draft November 22, 2026 19:26
@erikwb

erikwb commented Nov 22, 2026 •

Copy link
Copy Markdown
Author

Returning this PR to draft pending the AGX2 (M1 Pro / M2) hardware conformance and same-machine texture-heavy compute A/B runs, while keeping ca677d21601b unchanged so those runs and any A18 Pro / J616s / G17C follow-ups target the same SHA.

  • AGX2 (M1 Pro / M2) blocker: The 160-half cap applies to AGX2 by default and we do not have M1 Pro or M2 hardware in this lab, so this remains in draft pending AGX2 dEQP-VK.compute.*, dEQP-VK.texture.*, and dEQP-VK.image.* conformance and a same-machine texture-heavy compute A/B (alongside the A18 Pro / J616s zero-change shader-stats diff).
  • Non-blocking agx_performance.c note: Keeping if (dev->emit_agx3d || dev->encoding == AGX_ISA_ENCODING_AGX3D) (return 0;) at src/asahi/compiler/agx_performance.c:101-102 for now so ca677d21601b does not churn before the AGX2 run; we can drop it cleanly if a follow-up push is needed after AGX2 results arrive.
  • Current aurora-wip check: Verified ca677d21601b still merges cleanly onto origin/aurora-wip (f0ae30b1797) with no conflicts. On origin/aurora-wip (f0ae30b1797), hk_physical_device_compiler_flags() (src/asahi/vulkan/hk_shader.c:208-214) only combines pdev->dev.debug (bits up to AGX_DBG_VERIFY at bit 25, src/asahi/lib/agx_device.h:84) and hk_fs_structure_key_bits() (bit 30, src/asahi/vulkan/hk_shader.c:204-205), leaving bit 31 free; on the PR head (ca677d21601b), hk_physical_device_compiler_flags() occupies bit 31 with ((uint64_t)launch_scratch << 31) at src/asahi/vulkan/hk_shader.c:214-216. Updated the PR description (## Testing and ## Still to run) to record the ca677d21601b (parent afcc08b71067) test build and the return to draft.

@RyanTheTide

Copy link
Copy Markdown

We have an M2 and an M1 Pro, so the AGX2 row is ours. Queued at ca677d2: dEQP-VK.compute.* plus a texture-heavy compute A/B, base vs head. We'll post it here.

Assisted-by: LLM (Claude:Opus 5.5)

@erikwb

erikwb commented Nov 22, 2026 •

Copy link
Copy Markdown
Author

Completed the fuller J716c (M5 Max, G17C) dEQP-VK.compute.* and dEQP-VK.texture.* CTS comparison on ca677d21601b against parent afcc08b71067 (8,508 Pass, 0 Fail, 0 diffs across 15,000 sampled cases, 0 GPU faults) and updated the PR body (## Testing and ## Still to run).

  • Configuration: J716c (G17C), Linux 7.3.0-ewb270+, AArch64 release Borealis cross-build with per-commit asahi_clc, MESA_SHADER_CACHE_DISABLE=true.
  • dEQP-VK.compute.* (5,000-case sample): base afcc08b71067 Pass=1274, NotSupported=3726; head ca677d21601b Pass=1274, NotSupported=3726; 0 diffs, 0 GPU errors/faults in dmesg.
  • dEQP-VK.texture.* (10,000-case sample): base afcc08b71067 Pass=7234, NotSupported=2766; head ca677d21601b Pass=7234, NotSupported=2766; 0 diffs, 0 GPU errors/faults in dmesg.
  • System health: 0 new failed units (1 pre-existing aurora-peripherals.service failed at boot; SELinux N/A on Arch ARM).

The PR remains in draft on ca677d21601b pending the blocking AGX2 (M1 Pro / M2) conformance and same-machine texture-heavy compute A/B runs, plus zero-change shader-stats confirmation on G17P (A18 Pro and J616s M4 Pro).

@RyanTheTide

Copy link
Copy Markdown

M2 (AGX2, G14) row at ca677d2.

Base afcc08b vs head ca677d2, Borealis (the AGX2 A/B for the 160-half cap):

  • dEQP-VK.compute.*: 3,071 cases, 457 pass, 2,614 not supported on both, 0 changed.
  • gpubench-vk, GPU time median of 3 interleaved reps, 1080p, queue depth 1 / 4: conv2d gaussian 1573.9 -> 1574.0 / 1576.7 -> 1576.6 us, conv2d bilateral 2499.4 -> 2499.4 / 2499.7 -> 2499.5 us, edge sobel 694.0 -> 685.2 / 694.4 -> 695.2 us, edge harris 1077.7 -> 1072.5 / 1068.7 -> 1068.0 us. All within -1.3% to +0.1%, inside the spread between reps, and every output CRC matches.

So no regression on AGX2. These kernels do not show a gain either. I did not check whether any of them crosses the cap, so this is a no-regression row, not a performance one.

Environment: M2 (G14G), Fedora Aurora, kernel 7.3.0-aurora-fleet82, same boot for both arms, Mesa built from each sha and loaded per test command (nothing installed), dEQP 4 processes in 500-case chunks, GL surfaceless pbuffer 256x256 rgba8888d24s8ms0. Base and head order alternates between groups. No GPU firmware crash and no GPU fault or timeout in the kernel log during the dEQP rows. Trace: traces/gpu/20261123-r3-m2-batch2.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review at ca677d2. The cap comes from the encoding's register budget and occupancy. Please show that the AGX2 fallback declines the new target cleanly when it would introduce scratch, and attach the measured G14G performance pair. Separate the sampled compute workload from a pure image-store control.

Compute programs with four or more texture reads spend much of their
time waiting on them, and more resident threads hide that wait. Cap
their registers so a core keeps 5/8 of its maximum resident threads,
trading scratch spills for occupancy.

The cap is derived from the device's register file and the occupancy
steps rather than keyed on one chip. agx_register_file_halves() gives
the per-thread file for each encoding, and
agx_sampled_compute_register_cap() takes the largest budget that still
reaches the target step. The spills only pay for a large step in
resident threads, so a device gets the cap only where it raises them by
half again or more: 384 to 640 threads gained on G17C, while 512 to 640
lost 7-10% on G16G with the same sample workload. A device whose file is
unknown gets no cap. With the current occupancy table the AGX2 and AGX3C
encodings resolve to 160 halves (640 resident threads instead of 384);
AGX3B, whose file already reaches 512, gets none.

The cap only applies where the launch takes the scratch of its spills.
The driver says so with the new optional_spills key bit, from
hk_compute_launch_takes_scratch(), the same rule its compute dispatch
paths accept scratch by: G18G launches take none, and M5 launches take
it only with the kernel's compute resources. Gallium does not set the
bit. G18G gets no cap from the derivation either. Borealis's shader
cache identity includes that rule, so a binary cached with the cap is
not reused on a kernel whose launch refuses its spills.

Helpers, preambles, unspillable programs and ray-traversal programs
keep their hardware budget. The cap is only an occupancy trade, so the
retry after a failed allocation drops it and spills down from the whole
file instead of refusing a program that fits there.

A unit test pins the derived cap per encoding (160 on G17C and AGX2,
none on AGX3B or G18G) and checks on both G17C and G14G (AGX2) that
the first allocation spills to the cap, that the retry uses the whole
file with zero scratch, and that a key without optional_spills or with
fewer than four texture reads keeps the whole file.

Signed-off-by: Erik Bourget <erik@ebourget.net>
@erikwb
erikwb force-pushed the j716c/t2-sample-occupancy branch from ca677d2 to 0f3b08d Compare November 23, 2026 22:19

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict at b2392e5ccd5f

APPROVE — b2392e5ccd5f cleanly resolves the RA_MEM exhaustion and AGX3 scratch-limit fallback gap, drops the redundant AGX3D/emit_agx3d early return in agx_sampled_compute_register_cap(), merges cleanly onto aurora-wip, and passes all asahi/poly/SampledComputeRegisterCap.* unit tests plus live Apple M5 Max compute and texture Vulkan CTS with 0 failures and 0 GPU faults.

Blockers

None in code, host tests, or Apple M5 Max verification. (Note: the external AGX2 sampled-compute A/B and texture.*/image.* hardware rows listed in the PR's Not yet section remain outside our test machine's hardware scope.)

Checked

  • Changed files (10): src/asahi/compiler/agx_compile.c, agx_compile.h, agx_compiler.h, agx_performance.c, agx_register_allocate.c, meson.build, test/test-sampled-compute-register-cap.cpp, src/asahi/vulkan/hk_cmd_buffer.h, hk_cmd_dispatch.c, hk_shader.c.
  • Code audit:
    • agx_ra_impl() sets ctx->sampled_cap_applied only when agx_sampled_compute_register_cap() actually lowers max_possible_regs; agx_compile_function_nir_attempt() retries once from the full register file (disable_sampled_cap = true, ra_budget_cut = 0) on both ctx->ra_failed (including ra_mem_exhausted) and frame_B > limit_B before any ra_budget_cut retry or refusal.
    • agx_sampled_compute_register_cap() derives 160 purely from agx_register_file_halves() and the occupancy table (capped * 2 >= uncapped * 3), while hk_compute_launch_takes_scratch() unifies optional_spills, dispatch scratch refusals, and shader cache flag bit 31 (verified free on live aurora-wip 46b5217f8375, where AGX_DBG_VERIFY is bit 25 and FS structure is bit 30).
    • Clean Signed-off-by: on both commits (0f3b08dd0f90, b2392e5ccd5f) and clean git merge-tree --write-tree against 46b5217f8375.
  • Host build & tests (-Dbuildtype=debugoptimized -Db_ndebug=false, Linux 7.3 aarch64):
    • meson test --suite asahi: 236/236 OK (0 fail).
    • meson test --suite poly: 5/5 OK (0 fail).
    • agx_tests --gtest_filter='SampledComputeRegisterCap.*': 5/5 PASSED (78 ms, including RetryOnSpillExhaustionUsesWholeFile on both G14G/AGX2 and G17C/AGX3C).
  • Live Apple M5 Max Vulkan CTS (MESA_SHADER_CACHE_DISABLE=true, Linux 7.3):
    • dEQP-VK.compute.pipeline.basic.*: 79 Pass, 1 NotSupported, 0 Fail (80 total).
    • dEQP-VK.texture.filtering.2d.formats.*: 146 Pass, 94 NotSupported, 0 Fail (240 total).
    • 0 GPU faults in dmesg.
  • Untested scope: Live AGX2 (M1/M2) sampled-compute A/B performance pair and dEQP-VK.texture.*/dEQP-VK.image.* conformance; AGX3B (M4/A18 Pro) zero-change shader-stats confirmation.
Evidence
  • Audited full 10-file diff (707 lines) and verified hk_physical_device_compiler_flags() bit 31 availability on aurora-wip (46b5217f8375).
  • Built b2392e5ccd5f with -Dbuildtype=debugoptimized -Db_ndebug=false -Dvulkan-drivers=asahi -Dgallium-drivers=asahi -Dplatforms=wayland -Dglx=disabled -Dbuild-tests=true -Dtools=drm-shim.
  • meson test --suite asahi: 236/236 OK; --suite poly: 5/5 OK; agx_tests --gtest_filter='SampledComputeRegisterCap.*': 5/5 PASSED.
  • Live Apple M5 Max CTS: dEQP-VK.compute.pipeline.basic.* (79/80 Pass, 1 NotSupported, 0 Fail), dEQP-VK.texture.filtering.2d.formats.* (146/240 Pass, 94 NotSupported, 0 Fail), 0 GPU faults in dmesg.

Resolve conflicts in src/asahi/compiler/agx_register_allocate.c (keep the sampled compute cap alongside agx3_register_budget()) and src/asahi/vulkan/hk_cmd_dispatch.c (keep G18G scratch support up to 64 KiB via g18_compute_resources). Drop the stale AGX_CHIP_G18G early return in hk_compute_launch_takes_scratch() now that G18G compute launches accept scratch frames.

Signed-off-by: Erik Bourget <erik@ebourget.net>
hk_dispatch_g17g_native() is only called for AGX_CHIP_G18G, where hk_compute_launch_takes_scratch() unconditionally returns true and scratch_bytes > max_spill (64 KiB) is already checked directly. Drop the redundant !hk_compute_launch_takes_scratch() guard there and update the hk_compute_launch_takes_scratch() comment.

Also drop bit 31 from hk_physical_device_compiler_flags(): its sole consumer, hk_cache_identity(), already hashes both dev->chip and all 64 kernel feature bits in dev->params.features, which completely determine hk_compute_launch_takes_scratch().

Signed-off-by: Erik Bourget <erik@ebourget.net>
… cap

Hardware A/B testing of the 160-half sampled-compute register cap on G13G
(AGX2) and G17C (AGX3C) exposed two separate spill-regression causes, plus
a stage-scoping bug in hk_compile_nir():

1. ISA spill backing (AGX2 vs AGX3 static TPS): AGX2 spills to a
   helper-backed device-memory stack (agx_scratch) with explicit 2-slot
   scoreboard waits after each reload, whereas AGX3 uses launch-allocated
   static thread-private storage (copy/rld with hardware scoreboarding).
   On G13G, capping sampled compute to 160 halves slowed ALU-heavy sampled
   compute by +16% to +259% (up to 3.6x) and yielded no meaningful gain on
   texture-bound workloads (-0.9% to +2.4%, vs -7.0% to -14.5% on G17C for
   the same shaders). Restrict agx_sampled_compute_register_cap() to
   encodings with static TPS (AGX3B/AGX3C/AGX3D; AGX3B still resolves to 0
   from its 192-half register file).

2. Unbounded optional spills on AGX3C: applying the cap solely on
   samples >= 4 forced 82-290 spills/fills on ALU-heavy compute shaders
   with only 8 texture fetches, regressing G17C by +8.7% to +47.9%, and
   added 54-78 spills (216-312 B scratch) on heavy texture shaders for 0%
   gain. Add agx_sampled_cap_spills_excessive() and drop the optional cap
   via the existing whole-file retry in agx_compile_function_nir_attempt()
   when RA spills exceed 192 B, 48 scalar stores/loads, or 8 loop-weighted
   spill/fill ops per weighted texture op (counted per loop header).

3. Stage scoping in hk_compile_nir(): software vertex and tessellation-eval
   variants are retagged to MESA_SHADER_COMPUTE before backend key setup.
   Require sw_stage == MESA_SHADER_COMPUTE when setting optional_spills so
   only real Vulkan compute shaders opt into the cap.

Signed-off-by: Erik Bourget <erik@ebourget.net>
@erikwb
erikwb requested a review from a team as a code owner November 26, 2026 05:34
@erikwb

erikwb commented Nov 26, 2026 •

Copy link
Copy Markdown
Author

Merged aurora-wip (262403b7bce1), dropped the redundant G18G scratch guard and bit-31 compiler flag (a064bf402c15), and fixed two spill-regression causes plus a stage-scoping bug uncovered by live J313 (G13G) and J716c (G17C) paired A/B testing (aca2656bc5b1):

  1. Static-TPS ISA requirement (src/asahi/compiler/agx_performance.c): Live J313 (G13G / AGX2) A/B testing on a064bf402c15 showed the 160-half cap regressing ALU-heavy sampled compute by +16% to +259% (up to 3.6x, vs +8.7% to +47.9% on G17C) and yielding no meaningful gain on texture-bound compute (-0.9% to +2.4%, vs -7.0% to -14.5% on G17C for the same shaders). AGX2 lowers spills to a helper-backed device-memory scratch BO (agx_scratch) with explicit 2-slot scoreboard wait stalls after each reload, whereas AGX3 uses launch-allocated static TPS (copy/rld). agx_sampled_compute_register_cap() now requires agx_isa_has_static_tps(dev->encoding) (AGX3B/AGX3C/AGX3D), so AGX2 resolves to 0 (13/13 test shaders byte-identical to base on ASAHI_SHIM_CHIP=g13g) while AGX3C/AGX3D keep 160 and AGX3B remains 0 from its 192-half file.
  2. Post-spill cost guard (src/asahi/compiler/agx_register_allocate.c, agx_compile.c): Even on J716c (G17C), capping solely on samples >= 4 forced 82–290 spills/fills on ALU-heavy shaders with 8 texture reads (+8.7% to +47.9% slower) and 54–78 spills (216–312 B scratch) on heavy texture shaders for 0% gain. Added agx_sampled_cap_spills_excessive() to drop the optional cap via the existing whole-file retry whenever RA spills exceed 192 B, 48 scalar stores/loads, or 8 loop-weighted spill/fill ops per weighted texture op (counted per loop header, ignoring NIR scratch < spill_base_B).
  3. Stage scoping (src/asahi/vulkan/hk_shader.c): hk_compile_nir() now requires sw_stage == MESA_SHADER_COMPUTE when setting optional_spills, so software vertex/tessellation-evaluation variants lowered to MESA_SHADER_COMPUTE are never opted into the compute occupancy cap.
  4. Exact-head verification at aca2656bc5b1 vs 262403b7bce1:
    • Host (x86_64): meson test --suite asahi --suite poly 327/327 passed (agx_tests 1,621 passed / 1 pre-existing skip; SampledComputeRegisterCap.* 9/9 passed, covering AGX2 no-cap, 0/3 texture boundary, excessive-spill retry, moderate-spill retention, multi-backedge and nested loops, non-zero spill_base_B, and 64-bit scalar boundary).
    • J716c (M5 Max / G17C B1, git-aca2656bc5 vs git-262403b7bc):
      • Paired A/B (6 interleaved rounds × 20 reps, cache off, diagnostics off, bit-identical outputs): sc_sampled_n24..40 regressions eliminated (+8.7%..+47.9% -> -3.7%..+1.7%, uncapped); tb_sampled_n40/48 uncapped (-0.1% / +0.2%, no wasted scratch); texture-bound wins preserved (tb_sampled_n24 -11.1% 6/6, tb_sampled_n32 -14.5% 6/6, samp_sampled -7.0% 6/6, samp_control -0.0%).
      • CTS (VK-GL-CTS 7ba03cdac7ae): dEQP-VK.compute.* (60,838: 15,525 Pass / 45,313 NotSupported) + dEQP-VK.texture.* (27,301: 19,440 Pass / 7,861 NotSupported) = 88,139 cases ran with 0 diffs, 0 GPU error lines in dmesg, 0 AVC lines (SELinux N/A), 0 new failed units (2 pre-existing host units unchanged).
    • J713 (M4 Air / G16G B0, AGX3B, cap = 0, git-aca2656bc5 vs git-262403b7bc):
      • CTS (VK-GL-CTS 7ba03cdac7ae): dEQP-VK.compute.* (60,838: 13,140 Pass / 47,698 NotSupported / 0 Fail, 0 diffs) + dEQP-VK.texture.* (27,301: 14,724 Pass / 12,577 NotSupported / 0 Fail after isolated rerun of 6 symmetric parallel-shard flakes in explicit_lod.2d.formats.*, 6/6 Pass on both arms isolated, 0 net diffs) = 88,139 cases ran with 0 net diffs.
      • Uncached AGX_MESA_DEBUG=shaderdb: 9/9 compute probes + 85/85 dEQP-VK.compute.pipeline.basic.* shaders byte-identical (0 diffs).
      • Health: 0 GPU error lines, 0 AVC (SELinux N/A), 0 failed units before and after.
    • Remaining under **Not yet:**: Live J313 (G13G) re-run at aca2656bc5b1 (lab J313 currently offline awaiting physical power cycle; AGX2 statically gated off and byte-identical on drm-shim), J716c Cyberpunk 2077 T2 A/B on AC (lab J716c currently on battery, macsmc-ac online=0), and external G18G (M6) spilling launch verification.

Signed-off-by: Erik Bourget <erik@ebourget.net>

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at c2984e1f

Blockers: none (ready for multi-family lab regression overlay and merge).

Checked:

  • c2984e1f cleanly merges aurora-wip over aca2656b, which resolved the earlier AGX2 spill regression by requiring static-TPS ISA support (agx_performance.c) and bounding optional spills while dropping the redundant G18G scratch check and compiler flag bit (a064bf40).
  • Exact-head hardware verification on G17C (J716c: 88,139 compute+texture CTS cases with 0 diffs, -7.0% to -14.5% sampled-compute wins, 0.0% control delta, 0 GPU faults) and G16G (J713: 88,139 compute+texture CTS cases with 0 net diffs, byte-identical uncached shaderdb, 0 GPU faults).
  • Checked for Apple material, tool attribution, private serials, and Borealis naming: clean; human Signed-off-by present; MERGEABLE on aurora-wip.
Evidence
  • Head: c2984e1f (base: aurora-wip, mergeable: MERGEABLE, +858/-5).

@erikwb

erikwb commented Nov 28, 2026

Copy link
Copy Markdown
Author

Verified host build and unit tests at c2984e1f32a5 (clean merge of aurora-wip babd36d4d477 over aca2656bc5b1, keeping the PR's 10-file diff intact and merging cleanly against current aurora-wip 46f3a68e60d9):

  • meson test -C _build --suite asahi --suite poly: 328/328 passed (agx_tests: 1,640 passed, 1 pre-existing skipped NativeFMA.ExactDyadicOracle; SampledComputeRegisterCap.*: 9/9 passed).
  • SHA clarification on the hardware rows: the prior-head aca2656bc5b1 vs 262403b7bce1 J716c (including sampled-compute timing) and J713 hardware rows remain preserved in Evidence as pre-merge results alongside the new exact-head c2984e1f32a5 vs babd36d4d477 J716c and J713 runs below; exact-head J313 and the unrun J716c timing items remain under **Not yet:**.
  • PR body: condensed **Tested:** (detailed tables/rows remain in Evidence), restored the aca2656bc5b1 host test row (327/327 and 1,621 passed) in Evidence, corrected agx_ra() and the independent 48-store / 48-load spill guard wording in Details/Risk, recorded the c2984e1f32a5 host verification and git revert -m 1 <merge-sha> revert procedure, and kept the aca2656bc5b1 hardware results labeled as pre-merge.
  • Exact-head J716c & J713 (2026-11-28): c2984e1f32a5 vs babd36d4d477 (native release base-plus-patch builds identified by source SHA and libvulkan_asahi.so SHA-256, sharing git-97742505d8 in driverInfo):
    • J716c (M5 Max / G17C B1): dEQP-VK.compute.* (60,838) and dEQP-VK.texture.* (27,301) 0 diffs, texture shaderdb reported identical per-case stats and totals (30,616 shaders, 0 of 27,301 cases changed, 0 spills), boot/smoke clean. The paired sc_bench/m5probe sampled-compute probes did not run because the probe sources/binaries were unavailable on the Mac and build host, and the Cyberpunk 2077 T2 A/B did not run because it did not fit the pass; both remain under **Not yet:**.
    • J713 (M4 Air / G16G B0 / AGX3B): dEQP-VK compute + texture all 88,139 cases (27,864 Pass / 60,275 NotSupported, 0 diffs), --shaderdb across 45,341 shaders byte-identical (+0 on every total, 0 of 88,139 cases changed, expected with cap = 0), default-boot baseline smoke clean (0 new dmesg errors during this PR's Mesa run window). J313 exact-head row remains pending under **Not yet:**.

@RyanTheTide
RyanTheTide dismissed DjDeveloperr’s stale review November 29, 2026 01:35

The cap now requires static TPS support and bounds optional spills. This excludes the AGX2 path from the cap and addresses the allocation fallback finding. Exact-head host, G17C and G16G results are recorded in the PR. aca2656b

@RyanTheTide

Copy link
Copy Markdown

The newly merged #375 overlaps #461 in agx_compile_function_nir. I have a local forward-merge preview that keeps the sampled-cap retry and the shifted-half packer retry independent, and I’m running both test groups. Please forward-merge aurora-wip if you are already working on this. If the branch is still unchanged at 14:08 AEDT tomorrow, we will carry the tested resolution forward. The J873g direct and indirect spill readback is still queued.

Resolve the agx_compile_function_nir() overlap with #375. The function
now takes both disable_sampled_cap and the shifted-half select fold
arguments. The three sampled-cap retries (RA failure, excessive spills,
scratch frame over the target limit) and the spill-budget retry pass
the fold arguments through unchanged, and the caller's retries without
the fold start with the sampled cap allowed again, so the two retries
stay independent.

Signed-off-by: Erik Bourget <erik@ebourget.net>
@erikwb

erikwb commented Nov 29, 2026

Copy link
Copy Markdown
Author

Forward-merged aurora-wip (06e34ecacbf3, which includes #375) into this branch as 6f1bba6b4335 (merge commit, no rebase).

  • Conflict: only agx_compile_function_nir() in src/asahi/compiler/agx_compile.c. It now takes disable_sampled_cap and agx: select the half of a word shifted by 0 or 16 post-RA #375's opt_select_shifted_half / did_select_shifted_half together. The three sampled-cap retries (allocation failure, excessive spills, scratch frame over the target limit) and the spill-budget retry pass the fold arguments through unchanged. The caller's retries without the shifted-half fold (plain and float-constants-in-registers) start with disable_sampled_cap = false, so the two retries stay independent. Every sampled-cap retry runs before the fold, and the flag is cleared on entry, so a cap retry never reports a stale fold.
  • Tested at 6f1bba6b4335 (host x86_64, gallium-drivers=asahi, vulkan-drivers=asahi, build-tests=true, tools=drm-shim): meson test --suite asahi --suite poly: 356/356 passed. agx_tests: 1,828 passed, 1 skipped (the existing NativeFMA.ExactDyadicOracle skip). SampledComputeRegisterCap.* 9/9 and SelectShiftedHalf.* 4/4 passed.
  • No new hardware runs since the merge. The PR's own diff against the base is unchanged apart from the merged signature (10 files).

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 6f1bba6b4335

Blockers: None.

Checked:

  • Forward merge 6f1bba6b4335 of aurora-wip (06e34ecacbf3, including #375) over previously approved c2984e1f:
    • src/asahi/compiler/agx_compile.c (agx_compile_function_nir() and agx_compile_shader_nir_internal()): cleanly keeps disable_sampled_cap and #375's opt_select_shifted_half/did_select_shifted_half independent — all three sampled-cap retries (ra_failed, excessive spills, scratch frame over target limit) and the spill-budget retry preserve the fold arguments unchanged, while the caller's no-fold retries restart with disable_sampled_cap = false.
  • Exact-head host tests at 6f1bba6b4335: meson test --suite asahi --suite poly 356/356, agx_tests 1,828 pass / 1 skip, SampledComputeRegisterCap.* 9/9, SelectShiftedHalf.* 4/4.
  • Driver logic outside the merged signature is unchanged from c2984e1f, so the prior G17C (J716c) and G16G hardware rows stand.
Evidence
  • Verified all call sites of agx_compile_function_nir() pass disable_sampled_cap, opt_select_shifted_half, and did_select_shifted_half in matching parameter order.
  • MERGEABLE on aurora-wip; no code blockers found at 6f1bba6b4335.

@RyanTheTide

Copy link
Copy Markdown

Thanks for carrying the forward merge through. I verified 6f1bba6b4335 against aurora-wip at 06e34ecacbf3. The assertions-enabled AArch64 build passed. The independent host Asahi and Panfrost suites passed 353/353, including all 13 targeted sampled-cap and shifted-half tests.

On J700 (G17P), the two Vulkan drivers matched all 51 compute, sampler and descriptor CTS results (43 Pass and 8 NotSupported per arm). One pinned case was absent in this CTS build. The saved boot and host health stayed unchanged, with no GPU faults in the run window. This row covers the unaffected path.

The remaining target run is J873g on a compatible submit kernel. It will check whether cap-positive direct and indirect dispatches spill and return the expected data, with pure image-store as a control. The paired drivers are staged at this exact revision. GitHub rejected the build/test dispatch on this fork, so the exact-head CI run is being routed through an enabled runner.

@RyanTheTide
RyanTheTide merged commit 688f852 into aurora-wip Nov 29, 2026
@RyanTheTide
RyanTheTide deleted the j716c/t2-sample-occupancy branch November 29, 2026 06:19
@RyanTheTide

Copy link
Copy Markdown

Thanks for the occupancy fix and for carrying the forward merge through. This landed at 688f852f67ad. The landed tree matches the tested head and the M5 Max, M4 Air and J700 results stand. I opened #1333 to track the G18G-specific spill readback. We’ll pick up the follow-up, and you’re welcome to send the PR if you’d like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants